package com.irilya.youtubeevents.gwt.client;


import com.irilya.youtubeevents.gwt.client.model.YouTubePlayer;
import com.irilya.youtubeevents.gwt.client.service.YouTubePlayerService;
import com.irilya.youtubeevents.gwt.client.service.ServiceProvider;

public class YouTubePlayerHandler
{

    private YouTubeEventsContext youTubeEventContext;

    public YouTubePlayerHandler(YouTubeEventsContext youTubeEventsContext)
    {

        this.youTubeEventContext = youTubeEventsContext;

        getYouTubePlayerService().exposeOnYouTubePlayerReady();
        YouTubePlayer player = this.youTubeEventContext.getYouTubePlayer();
        buildYouTubePlayer(player);
        embedYouTubeVideo(player);
    }

    public void play()
    {
        getYouTubePlayerService().play(this.youTubeEventContext.getYouTubePlayer().getPlayerId());
    }

    public void stop()
    {
        getYouTubePlayerService().stop(this.youTubeEventContext.getYouTubePlayer().getPlayerId());
    }

    private void embedYouTubeVideo(YouTubePlayer player)
    {
        getYouTubePlayerService().embedYouTubeVideo(player.getPlayerUrl(), player.getPlayerId(), player.getPlayerDivId(), player.getWidth(), player.getHeight());
    }

    private YouTubePlayerService getYouTubePlayerService() {
        return (YouTubePlayerService) ServiceProvider.getInstance().getService("playerService");
    }

    private void buildYouTubePlayer(YouTubePlayer player)
    {

        player.setPlayerUrl("http://www.youtube.com/v/EYNySS2VjDI&amp");
        player.setPlayerId("youTubePlayerId");
        player.setPlayerDivId("youtubePlayerDiv");
        player.setHeight("344");
        player.setWidth("425");
    }
}
